package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.AccidentReportInkuryUser;
import com.jhgsys.internal.accident.mapper.AccidentReportInkuryUserMapper;
import com.jhgsys.internal.accident.service.IAccidentReportInkuryUserService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zjb
 * @date 2024-09-13 10:27:18
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportInkuryUserServiceImpl extends ServiceImpl<AccidentReportInkuryUserMapper, AccidentReportInkuryUser> implements IAccidentReportInkuryUserService {

    /**
     * 查询（分页）
     * @author zjb
     * @date 2024-09-13 10:27:18
     * @param request QueryRequest
     * @param accidentReportInkuryUser accidentReportInkuryUser
     * @return IPage<AccidentReportInkuryUser>
    */
    @Override
    public IPage<AccidentReportInkuryUser> findAccidentReportInkuryUserPage(QueryRequest request, AccidentReportInkuryUser accidentReportInkuryUser) {
        LambdaQueryWrapper<AccidentReportInkuryUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportInkuryUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportInkuryUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zjb
     * @date 2024-09-13 10:27:18
     * @param accidentReportInkuryUser accidentReportInkuryUser
     * @return List<AccidentReportInkuryUser>
     */
    @Override
    public List<AccidentReportInkuryUser> findAccidentReportInkuryUserList(AccidentReportInkuryUser accidentReportInkuryUser) {
	    LambdaQueryWrapper<AccidentReportInkuryUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportInkuryUser::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zjb
    * @date 2024-09-13 10:27:18
    * @param id
    * @return AccidentReportInkuryUser
    */
    @Override
    public AccidentReportInkuryUser findAccidentReportInkuryUserById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zjb
     * @date 2024-09-13 10:27:18
     * @param accidentReportInkuryUser accidentReportInkuryUser
     */
    @Override
    @Transactional
    public boolean createAccidentReportInkuryUser(AccidentReportInkuryUser accidentReportInkuryUser) {
        return this.save(accidentReportInkuryUser);
    }

    /**
     * 修改
     * @author zjb
     * @date 2024-09-13 10:27:18
     * @param accidentReportInkuryUser accidentReportInkuryUser
     */
    @Override
    @Transactional
    public boolean updateAccidentReportInkuryUser(AccidentReportInkuryUser accidentReportInkuryUser) {
        return this.updateById(accidentReportInkuryUser);
    }

    /**
     * 逻辑删除
     * @author zjb
     * @date 2024-09-13 10:27:18
     * @param accidentReportInkuryUser accidentReportInkuryUser
     * @param accidentReportInkuryUserIds accidentReportInkuryUserIds
     */
    @Override
    @Transactional
    public boolean deleteAccidentReportInkuryUser(AccidentReportInkuryUser accidentReportInkuryUser,String accidentReportInkuryUserIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(accidentReportInkuryUserIds.split(StringPool.COMMA));

        accidentReportInkuryUser.setDeletedBy(user.getUserName());
        accidentReportInkuryUser.setDeletedTime(new Date());
        accidentReportInkuryUser.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(accidentReportInkuryUser,new LambdaQueryWrapper<AccidentReportInkuryUser>().in(AccidentReportInkuryUser::getReportInkuryUserId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author zjb
    * @date 2024-09-13 10:27:18
    * @param accidentReportInkuryUserIds accidentReportInkuryUserIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteAccidentReportInkuryUser(String accidentReportInkuryUserIds) {
        List<String> list = Arrays.asList(accidentReportInkuryUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportInkuryUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportInkuryUser::getReportInkuryUserId,list);
        return this.remove(wapper);
    }

    /**
     * 根据事故报告id删除
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param reportIds
     */
    @Override
    @Transactional
    public void deleteByReportIds(String reportIds) {
        List<String> list = Arrays.asList(reportIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportInkuryUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AccidentReportInkuryUser::getReportId,list);
        this.remove(wrapper);
    }
}
